##replication of figure 1##

setwd("C:/Replication Code/");

install.packages("bunching","ggplot2");

library(bunching);
library(ggplot2);


data<-read.csv("data.csv"); #read normal disaster loan#


##BPDL##


data$BPDLAmount<-data$ApprovedAmountRealEstate+data$ApprovedAmountContent


dataBPDL1<-subset(data,data$PDBL==4);


dataBPDL13<-subset(dataBPDL1,dataBPDL1$Year>=2014);

dataBPDL12<-subset(dataBPDL1,dataBPDL1$Year<2014&dataBPDL1$Year>=2008);

dataBPDL0307<-subset(dataBPDL1,dataBPDL1$Year<2008&dataBPDL1$Year>=2003);

dataBPDL131<-subset(dataBPDL13,dataBPDL13$Year==2014|dataBPDL13$Year==2015);

dataBPDL132<-subset(dataBPDL13,dataBPDL13$Year>2015);

dataBPDL1311<-subset(dataBPDL131,dataBPDL131$FEMADisasterNumber=="");
dataBPDL1312<-subset(dataBPDL131,dataBPDL131$FEMADisasterNumber!="");

dataBPDL1420<-rbind(dataBPDL132,dataBPDL1312);

dataBPDL0813<-rbind(dataBPDL1311,dataBPDL12);



##binned data for dataBPDL0307##

a=10000;
b=55500;
zstar=10000;
x0=500;
binwidth=x0;

binned_data0307 <- bunching::bin_data(z_vector =dataBPDL0307$BPDLAmount,zstar =zstar, binwidth = x0,
                          bins_l =a/x0, bins_r =b/x0);


bin<-seq(500,65000,by=x0);
bin<-as.data.frame(bin);
binned_data03070<-merge(bin,binned_data0307,by="bin", all=TRUE);
 binned_data03070$freq<-ifelse(is.na(binned_data03070$freq),0,binned_data03070$freq);
 binned_data03070$freq_orig<-ifelse(is.na(binned_data03070$freq_orig),0,binned_data03070$freq_orig);
 binned_data03070$z<-ifelse(is.na(binned_data03070$z),binned_data03070$bin,binned_data03070$z);

binned_data03070$pfreq<-100*binned_data03070$freq/sum(binned_data03070$freq);

##binned data for dataBPDL0813##

a=14000;
b=51500;
zstar=14000;
x0=500;
binwidth=x0;


binned_data0813 <- bunching::bin_data(z_vector = dataBPDL0813$BPDLAmount,zstar =zstar, binwidth = x0,
                          bins_l =a/x0, bins_r =b/x0);

binned_data0813$pfreq<-100*binned_data0813$freq/sum(binned_data0813$freq);

##binned data for dataBPDL1420##


a=25000;
b=40500;
c=0;
zstar=25000;
x0=500;
binwidth=x0;


y0=5;

binned_data1420 <- bunching::bin_data(z_vector = dataBPDL1420$BPDLAmount,zstar =zstar, binwidth = x0,
                          bins_l =a/x0, bins_r = b/x0);

binned_data14200<-merge(bin,binned_data1420,by="bin", all=TRUE);
 binned_data14200$freq<-ifelse(is.na(binned_data14200$freq),0,binned_data14200$freq);
 binned_data14200$freq_orig<-ifelse(is.na(binned_data14200$freq_orig),0,binned_data14200$freq_orig);
 binned_data14200$z<-ifelse(is.na(binned_data14200$z),binned_data14200$bin,binned_data14200$z);

binned_data14200$pfreq<-100*binned_data14200$freq/sum(binned_data14200$freq);



plot_binned_data<-rbind(binned_data03070,binned_data0813,binned_data14200);
plot_binned_data$cohart<-c(rep("$10000(2003-2007)",nrow(bin)),rep("$14000(2008-2013)",nrow(bin)),rep("$25000(2014-2020)",nrow(bin)))

LegendTitle= "Collateralization Cut-off"
linetype = c('dotted', 'dashed','solid');
colortype=c("grey30", "#00BA38","#EF8A62");


pdf(width = 8, # The width of the plot in inches
    height = 4) # The height of the plot in inches

 ggplot(plot_binned_data, aes(x=bin,y=pfreq, group = cohart)) +
  geom_line(aes(linetype = cohart, col = cohart)) +
  scale_color_manual(name = LegendTitle, values = colortype) +
  scale_linetype_manual(name = LegendTitle, values = linetype) +
 scale_x_continuous(breaks=c(5000,15000,25000,35000,45000,55000,65000))+ 
 scale_y_continuous(breaks=c(2.5,5,7.5,10))+
theme_bw()+
 labs(x="Loan Amount",y="Percentage of Loans")+
theme(legend.position = c(0.8,0.8))+
  theme(plot.title = element_text(hjust = 0.5))+
 theme(axis.line = element_line(color='black'),
    plot.background = element_blank(),
   panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank())

dev.off()







